package uit.nfcmenu.manager;

import java.net.MalformedURLException;
import java.net.URL;

import uit.nfcmenu.db.model.ItemModel;
import uit.nfcmenu.util.Download;
import uit.nfcmenu.util.FileCache;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;


public class WaiterGetImageInBgAsyncTask extends AsyncTask<Void, Void, Void> {
	String tag = WaiterGetImageInBgAsyncTask.class.getSimpleName();
	
	ItemModel mItem;
	
	public WaiterGetImageInBgAsyncTask(Context context, ItemModel item) {
		mItem = item;
		Log.i(tag, "Download and save image " + mItem.getId());
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	@Override
	protected Void doInBackground(Void... params) {
		Log.i(tag, "Download..." + mItem.getId());
		
		try {
			new Download(new URL(mItem.getImageLink()), 
					FileCache.getItemSDCardUri(mItem.getId()));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}		
		return null;
	}
	
	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		Log.i(tag, "Download " + mItem.getId() + " done!");
		
	}
}
